﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace QuanLyKhachSan
{
    public partial class UC_UserGroup_Edit : System.Web.UI.UserControl
    {
        private int UserID = -1;
        private string ListGroupID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.Request.QueryString["UserID"] != null)
                {
                    UserID = int.Parse(this.Request.QueryString["UserID"].ToString());
                }
                if (!Page.IsPostBack)
                {
                    Group_Load();
                    User_Load();
                    cbxUser.SelectedValue = "1";
                    if (UserID != -1)
                    {
                        UserGroup_Load(); 
                    }
                }
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }
        /// <summary>
        /// UserGroup_Load
        /// </summary>
        protected void UserGroup_Load()
        {
            try
            {
                List<UserGroupInfo> list = new List<UserGroupInfo>();
                list = UserGroupBLL.UserGroup_GetItem(UserID);

                foreach (GridDataItem item in lstGroup.Items)
                {
                    foreach (UserGroupInfo ug in list)
                    {
                        if (int.Parse(item.OwnerTableView.DataKeyValues[item.ItemIndex]["GroupID"].ToString()) == ug.GroupID)
                        {
                            CheckBox checkBox = (CheckBox)item.FindControl("chkSelect");
                            checkBox.Checked = true;
                        }
                    }
                }

                cbxUser.SelectedValue = list[0].UserID.ToString();
                if (!string.IsNullOrEmpty(list[0].ExpiryDate))
                    rdpExpiryDate.SelectedDate = DateTime.Parse(list[0].ExpiryDate);
                if (!string.IsNullOrEmpty(list[0].EffectiveDate))
                    rdpEffectiveDate.SelectedDate = DateTime.Parse(list[0].EffectiveDate);
                if (list[0].IsTrialUsed == 1)
                    chkIsTrialUsed.Checked = true;
                else
                    chkIsTrialUsed.Checked = false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Group_Load
        /// </summary>
        protected void Group_Load()
        {
            try
            {
                lstGroup.DataSource = GroupBLL.Group_GetActivedItems();
                lstGroup.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// User_Load
        /// </summary>
        protected void User_Load()
        {
            try
            {
                cbxUser.DataSource = UserBLL.User_GetActivedItems();
                cbxUser.DataTextField = "UserName";
                cbxUser.DataValueField = "UserID";
                cbxUser.DataBind();
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }
        protected void cmdOK_Click(object sender, EventArgs e)
        {
            try
            {
                string value;
                foreach (GridDataItem gdi in lstGroup.Items)
                {
                    CheckBox chkSelect = gdi.FindControl("chkSelect") as CheckBox;
                    value = gdi.OwnerTableView.DataKeyValues[gdi.ItemIndex]["GroupID"].ToString();
                    if (chkSelect.Checked)
                    {
                        ListGroupID += value + ";";
                    }
                }

                int IsTrialUsed = 0;

                string expirydate = string.Empty;
                string effectivedate = string.Empty;

                if (!rdpExpiryDate.IsEmpty)
                    expirydate = rdpExpiryDate.SelectedDate.Value.ToString("MM/dd/yyyy");
                if (!rdpEffectiveDate.IsEmpty)
                    effectivedate = rdpEffectiveDate.SelectedDate.Value.ToString("MM/dd/yyyy");

                if (chkIsTrialUsed.Checked)
                    IsTrialUsed = 1;

                if (UserID == -1)
                    UserGroupBLL.UserGroup_Add(int.Parse(cbxUser.SelectedValue.ToString()), ListGroupID, expirydate, effectivedate, IsTrialUsed);
                else
                    UserGroupBLL.UserGroup_Add(int.Parse(cbxUser.SelectedValue.ToString()), ListGroupID, expirydate, effectivedate, IsTrialUsed);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
}
}
